package com.itfreer.ui.base.datetime;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.itfreer.ui.R;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.ISimpleView;

import java.util.Calendar;

/**
 * 定义时间选择控件
 */
public class TimePickerView extends LinearLayout
        implements TimePickerDialog.OnTimeSetListener, View.OnClickListener, ISimpleView, IPower {

    private TextView textView = null;
    private TimePickerDialog datePickerDialog;
    private String fieldName;
    private Object fieldValue;
    private String securityID;
    private boolean securityVisible=true;

    public TimePickerView(Context context) {
        super(context);
        setView(context);
    }

    public TimePickerView(Context context, AttributeSet attrs){
        super(context, attrs);
        setView(context);
        getAttrs(context, attrs);
    }

    public TimePickerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
        getAttrs(context, attrs);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        View view = View.inflate(context, R.layout.com_itfreer_core_ui_base_inputitem, null);

        textView = (TextView)view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_textview);
        textView.setOnClickListener(this);

        com.itfreer.ui.base.TextView imageView = (com.itfreer.ui.base.TextView) view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_down);
        imageView.setOnClickListener(this);

        Calendar c = Calendar.getInstance();
        datePickerDialog =  new TimePickerDialog(TimePickerView.this.getContext(), 0, TimePickerView.this,
                c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));

        LayoutParams param =
                new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(view, param);
    }

    @Override
    public void onClick(View v) {
        // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
        datePickerDialog.show();
    }

    @Override
    public void onDateSet(TimePicker timePicker, int hourOfDay, int minute) {
        fieldValue = String.format("%d:%d", hourOfDay, minute);
        textView.setText((String) fieldValue);
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        this.fieldValue = value;

        if(fieldValue==null){
            return;
        }

        String strValue = fieldValue.toString();
        String[] values = strValue.split(":");
        if(values.length!=2){
            return;
        }

        textView.setText((String)fieldValue);

        int hourOfDay = Integer.parseInt(values[0]);
        int minute = Integer.parseInt(values[1]);

        datePickerDialog.updateDate(hourOfDay, minute);
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        return fieldValue;
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }
}